package com.ymttest.database.sqlwapper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;

import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtAccountEntry;
import com.ymttest.database.model.withdraw.*;
import com.ymttest.utils.EnvSetup;

public class WithdrawWapper {
	SqlManager ymtr;
	SqlManager ymtfp;
	public WithdrawWapper() {
		if (ymtr == null || ymtr.getEnv() != "YmtRelease"  || ymtr.isClosed()) {
			ymtr = new SqlManager("YmtRelease");
		}
		if (ymtfp == null || ymtfp.getEnv() != "ymtfp" || ymtfp.isClosed()) {
			ymtfp = new SqlManager("ymtfp");
		}
	}

	/** 
	 * 查询Ymt_InvoiceFundFlowLogExample 表 资金流转操作日志表
	 * 
	 * @param ymtflowlog
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_InvoiceFundFlowLog> selectYmtInvoiceFundFlowLog(Ymt_InvoiceFundFlowLogExample ymtflowlog) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_InvoiceFundFlowLog> pia = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtInvoiceFundFlowLog", ymtflowlog);
		ymtfp.commit();
		return pia;
	}
	/**
	 * 单据操作日志表
	 * @param ymtflowlog
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_InvoiceOperationLog> selectYmtInvoiceOperationLog(Ymt_InvoiceOperationLogExample ymtflowlog) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_InvoiceOperationLog> pia = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtInvoiceOperationLog", ymtflowlog);
		ymtfp.commit();
		return pia;
	}
	/**
	 * 单据编号种子表
	 * @param ymtseedlog
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_InvoiceSerialSeed> selectYmtInvoiceSerialSeed(Ymt_InvoiceSerialSeedExample ymtseedlog) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_InvoiceSerialSeed> pia = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtInvoiceSerialSeed", ymtseedlog);
		ymtfp.commit();
		return pia;
	}
	/**
	 * 提现申请信息主表
	 * @param ymtseedlog
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_WithDrawInvoice> selectYmtWithDrawInvoice(Ymt_WithDrawInvoiceExample withdraw) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_WithDrawInvoice> pia = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtWithDrawInvoice", withdraw);
		ymtfp.commit();
		return pia;
	}
	
	public int  deleteByExample(Ymt_WithDrawInvoiceExample example) throws InterruptedException {

		int  count = ymtfp.delete("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.deleteByExample", example);

		ymtfp.commit();
		return count;
	}
	
	/**
	 * 提现申请幂等键记录表
	 * @param ymtide
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_WithDrawIdempotent> selectYmtWithDrawIdempotent(Ymt_WithDrawIdempotentExample ymtide) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_WithDrawIdempotent> pia = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtWithDrawIdempotent", ymtide);
		ymtfp.commit();
		return pia;
	}
	//selectYmtWithDrawInvoiceXloboLog
	/**
	 * 提现到贝海的日志
	 * @param ymtxlb
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_WithDrawInvoiceXloboLog> selectYmtWithDrawInvoiceXloboLog(Ymt_WithDrawInvoiceXloboLogExample ymtxlb) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_WithDrawInvoiceXloboLog> pia = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtWithDrawInvoiceXloboLog", ymtxlb);
		ymtfp.commit();
		return pia;
	}
	//selectYmtWithDrawXloboMapping
	/**
	 * 绑定的贝海帐号信息
	 * @param ymtxlb
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_WithDrawXloboMapping> selectYmtWithDrawXloboMapping(Ymt_WithDrawXloboMappingExample ymtxlb) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_WithDrawXloboMapping> pia = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtWithDrawXloboMapping", ymtxlb);
		ymtfp.commit();
		return pia;
	}
	//selectYmtAbroadFundAccount
	/**
	 * 查询国际银行卡信息
	 * @param fundacc
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_AbroadFundAccount> selectYmtAbroadFundAccount(Ymt_AbroadFundAccountExample fundacc) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_AbroadFundAccount> pia = ymtr.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtAbroadFundAccount", fundacc);
		ymtr.commit();
		return pia;
	}
	/**
	 * 查询国内银行卡信息
	 * @param fundacc
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_FundAccount> selectYmtFundAccount(Ymt_FundAccountExample fundacc) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_FundAccount> pia = ymtr.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtFundAccount", fundacc);
		ymtr.commit();
		return pia;
	}
	//selectYmtFundRateExpression
	/**
	 * 提现手续费表,按目标币种设置
	 * @param fundacc
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_fundRateExpression> selectYmtFundRateExpression(Ymt_fundRateExpressionExample fundacc) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_fundRateExpression> pia = ymtr.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtFundRateExpression", fundacc);
		ymtr.commit();
		return pia;
	}
	
	/**
	 * 提现申请信息扩展
	 * @param withdrawex
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_WithDrawInvoiceEx> selectYmtWithDrawInvoiceEx(Ymt_WithDrawInvoiceExExample withdrawex) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_WithDrawInvoiceEx> pia = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtWithDrawInvoiceEx", withdrawex);
		ymtfp.commit();
		return pia;
	}
	
	/** 
	 * 查询Ymt_InvoiceApprovmentExample 表 单据审核记录表
	 * 
	 * @param ymtflowlog
	 * @return
	 * @throws InterruptedException
	 */
	public List<Ymt_InvoiceApprovment> selectYmtInvoiceApprovment(Ymt_InvoiceApprovmentExample ymtApprovment) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Ymt_InvoiceApprovment> pia = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtInvoiceApprovment", ymtApprovment);
		ymtfp.commit();
		return pia;
	}
	
	public BigDecimal getSumFreezebyUserId(Integer userId) throws InterruptedException {
		@SuppressWarnings("unchecked")
		BigDecimal freeze=BigDecimal.ZERO;
		 List<Map> map = ymtr.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.getSumFreezebyUserId", userId);
		if(map.size()>0 && map.get(0)!=null)
			freeze=(BigDecimal) map.get(0).get("sumFreezeAmount"); 
		ymtr.commit();
		return freeze;
	}
	
	
	public List<Ymt_WithDrawInvoice>  getWithDrawInvoiceByUserId(Integer userId) throws InterruptedException {

		List<Ymt_WithDrawInvoice>  map = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.getWithDrawInvoiceByUserId", userId);

		ymtfp.commit();
		return map;
	}
	
	
	public List<Ymt_WithDrawInvoice>  getWithDrawInvoiceByHunter(Integer userId) throws InterruptedException {

		List<Ymt_WithDrawInvoice>  map = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.getWithDrawInvoiceByHunter", userId);

		ymtfp.commit();
		return map;
	}
	
	
	public List<Ymt_WithDrawInvoice>  getTotalWithDrawInvoiceByUserId(Integer userId) throws InterruptedException {

		List<Ymt_WithDrawInvoice>  map = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.getTotalWithDrawInvoiceByUserId", userId);

		ymtfp.commit();
		
		return map;
	}
	
	
	public Long  getTotalAmountWithDrawInvoiceByUserId(Integer userId) throws InterruptedException {
		Long totalAmoun=0L;
		List  map = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.getTotalAmountWithDrawInvoiceByUserId", userId);
        if(map.size()>0)
        	totalAmoun=(Long) map.get(0);
        ymtfp.commit();
		return totalAmoun;
	}
	
	//获取安利会提现计数
	public List<Ymt_WithDrawCounter> selectYmtWithDrawCounter(Ymt_WithDrawCounterExample example) throws InterruptedException {

		List<Ymt_WithDrawCounter>  map = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.selectYmtWithDrawCounter", example);

		ymtfp.commit();
		return map;
	}
	
	//修改安利会计数times
	public  int updateCounterByExampleSelective(Map<String, Object> parameter){

		int  map = ymtfp.update("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.updateByExampleSelective", parameter);

		ymtfp.commit();
		return map;
	}
	
	
	
	public  List<HashMap<String,Object>>  getTotalAmountWithDrawInvoiceByHunter(Integer userId) throws InterruptedException {

		List<HashMap<String,Object>> totalAmounts=new ArrayList();
		List<Map>  maplst = ymtfp.getSelectResults("com.ymttest.database.mapping.withdraw.YmtWithdrawMapper.getTotalAmountWithDrawInvoiceByHunter", userId);
        if(maplst.size()>0)
        	maplst.forEach(a->{
        		HashMap<String,Object> totalAmount=new HashMap<String,Object>();
        		totalAmount.put("AccountType",a.get("AccountType").toString());
        		totalAmount.put("totalAmount",a.get("totalAmount").toString());
        		totalAmounts.add(totalAmount);
        	});

        ymtfp.commit();
		return totalAmounts;
	}
	
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		ymtr.close();
		ymtfp.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		ymtr = null;
		ymtfp=null;
		super.finalize();
	}

	public static void main(String args[]) {
		try {
			WithdrawWapper WithdrawWapper=new WithdrawWapper();
			System.out.println("冻结金额："+WithdrawWapper.getSumFreezebyUserId(3383));
			
			
			System.out.println("selectYmtInvoiceFundFlowLogByExample");
			Ymt_InvoiceFundFlowLogExample ymtflowlog=new Ymt_InvoiceFundFlowLogExample();
			ymtflowlog.createCriteria().andInvoiceIdEqualTo("FKK2019041710184687").andUserIdEqualTo(3383).andAccountTypeEqualTo(1).andFundFlowLogIdEqualTo("e4cb628e-8ba6-4de6-b572-aa8c2f650eaa");
			ymtflowlog.setOrderByClause("createTime desc");
			List<Ymt_InvoiceFundFlowLog> flowlist = WithdrawWapper.selectYmtInvoiceFundFlowLog(ymtflowlog);
			flowlist.forEach(data->{
				System.out.println(FormatUtil.beanToGSONString(data));
				
			});
			System.out.println("Ymt_InvoiceOperationLogExample");
			Ymt_InvoiceOperationLogExample ymtoplog=new Ymt_InvoiceOperationLogExample();
			ymtoplog.createCriteria().andInvoiceIdEqualTo("FKK2019041710184687").andOperationLogIdEqualTo("0002e184-773c-40ab-967e-bbf91936c7fc");
			ymtoplog.setOrderByClause("operationtime desc");
			List<Ymt_InvoiceOperationLog> oplist = WithdrawWapper.selectYmtInvoiceOperationLog(ymtoplog);
			oplist.forEach(data->{
				System.out.println(FormatUtil.beanToGSONString(data));
				
			});
			
			System.out.println("Ymt_InvoiceSerialSeed");
			Ymt_InvoiceSerialSeedExample ymseedlog=new Ymt_InvoiceSerialSeedExample();
			ymseedlog.createCriteria().andSerialNumberEqualTo(10000656l);
			ymseedlog.setOrderByClause("createtime desc");
			List<Ymt_InvoiceSerialSeed> seedlist = WithdrawWapper.selectYmtInvoiceSerialSeed(ymseedlog);
			seedlist.forEach(data->{
				System.out.println(FormatUtil.beanToGSONString(data));
				
			});
			
			System.out.println("Ymt_WithDrawInvoiceExample");
			Ymt_WithDrawInvoiceExample wdlog=new Ymt_WithDrawInvoiceExample();
			wdlog.createCriteria().andApplyUserIdEqualTo(3383).andAccountTypeEqualTo(11).andWithDrawInvoiceIdEqualTo("FTX2017082310140239");
			wdlog.setOrderByClause("updatetime desc");
			List<Ymt_WithDrawInvoice> wdlist = WithdrawWapper.selectYmtWithDrawInvoice(wdlog);
			wdlist.forEach(data->{
				System.out.println(FormatUtil.beanToGSONString(data));
				
			});
			
			System.out.println("Ymt_WithDrawIdempotentExample");
			Ymt_WithDrawIdempotentExample ide=new Ymt_WithDrawIdempotentExample();
			ide.createCriteria().andWithDrawInvoiceIdEqualTo("FTX2019020110183029");
			ide.setOrderByClause("updatetime desc");
			List<Ymt_WithDrawIdempotent> idelist = WithdrawWapper.selectYmtWithDrawIdempotent(ide);
			idelist.forEach(data->{
				System.out.println(FormatUtil.beanToGSONString(data));
				
			});
			//selectYmtWithDrawInvoiceXloboLog
			System.out.println("Ymt_WithDrawInvoiceXloboLogExample");
			Ymt_WithDrawInvoiceXloboLogExample ymtxlb=new Ymt_WithDrawInvoiceXloboLogExample();
			ymtxlb.createCriteria().andWithDrawInvoiceIdEqualTo("FTX2017091210140449");
			ymtxlb.setOrderByClause("updatetime desc");
			List<Ymt_WithDrawInvoiceXloboLog> xlblist = WithdrawWapper.selectYmtWithDrawInvoiceXloboLog(ymtxlb);
			xlblist.forEach(data->{
				System.out.println(FormatUtil.beanToGSONString(data));
				
			});
			
			System.out.println("Ymt_WithDrawXloboMapping");
			Ymt_WithDrawXloboMappingExample ymtxlbmp=new Ymt_WithDrawXloboMappingExample();
			ymtxlbmp.createCriteria().andSellerIdEqualTo(3383);
			ymtxlbmp.setOrderByClause("updatetime desc");
			List<Ymt_WithDrawXloboMapping> xlbmplist = WithdrawWapper.selectYmtWithDrawXloboMapping(ymtxlbmp);
			xlbmplist.forEach(data->{
				System.out.println(FormatUtil.beanToGSONString(data));
				
			});
			
			
			System.out.println("Ymt_AbroadFundAccountExample");
			Ymt_AbroadFundAccountExample fundacc=new Ymt_AbroadFundAccountExample();
			fundacc.createCriteria().andSLoginIdEqualTo("wesper").andIActionEqualTo(0).andSAccountNameEqualTo("dw");
			fundacc.setOrderByClause("daddtime desc");
			List<Ymt_AbroadFundAccount> falist = WithdrawWapper.selectYmtAbroadFundAccount(fundacc);
			falist.forEach(data->{
				System.out.println(FormatUtil.beanToGSONString(data));
				
			});
			
			System.out.println("Ymt_FundAccountExample");
			Ymt_FundAccountExample fund=new Ymt_FundAccountExample();
			fund.createCriteria().andSLoginIdEqualTo("wesper").andIActionEqualTo(0).andSAccountNameEqualTo("支付宝001");
			fund.setOrderByClause("daddtime desc");
			List<Ymt_FundAccount> flist = WithdrawWapper.selectYmtFundAccount(fund);
			flist.forEach(data->{
				System.out.println(FormatUtil.beanToGSONString(data));
				
			});
			
			System.out.println("Ymt_fundRateExpression");
			Ymt_fundRateExpressionExample frund=new Ymt_fundRateExpressionExample();
			frund.createCriteria().andRateIdEqualTo(1);
			frund.setOrderByClause("updatetime desc");
			List<Ymt_fundRateExpression> frlist = WithdrawWapper.selectYmtFundRateExpression(frund);
			frlist.forEach(data->{
				System.out.println(FormatUtil.beanToGSONString(data));
				
			});
			
			System.out.println("Ymt_WithDrawInvoiceExample");
			Ymt_WithDrawInvoiceExExample wdex=new Ymt_WithDrawInvoiceExExample();
			wdex.createCriteria().andWithDrawInvoiceIdEqualTo("FTX2017082910140341");
			wdex.setOrderByClause("createtime desc");
			List<Ymt_WithDrawInvoiceEx> wdexlist = WithdrawWapper.selectYmtWithDrawInvoiceEx(wdex);
			wdexlist.forEach(data->{
				System.out.println(FormatUtil.beanToGSONString(data));
				
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
